package com.kerrykidz.system.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.rdp.util.config.SpringUtil;

public class SystemParaSingleton {

	private SystemParaSingleton() {
		content = new HashMap<String, String>();
		initLoadPara();
	}

	private Map<String, String> content;

	private static SystemParaSingleton instance;

	private static Object x = 0;

	// SPRING容器加载之后调用该实例
	public static SystemParaSingleton getInstance() {

		if (SystemParaSingleton.instance == null)
			synchronized (x) {
				if (SystemParaSingleton.instance == null)
					instance = new SystemParaSingleton();
			}
		return instance;
	}

	public String getValue(String key) {
		return this.content.get(key);
	}

	private void initLoadPara() {
		JdbcTemplate jdbcTemplate = SpringUtil.getBeanByType(JdbcTemplate.class);
		if (jdbcTemplate == null) {
			instance = null;
			return;
		}
		String sql = "SELECT PK_PARA_ID,PARA_VALUE FROM SYS_PARA";
		List<Map<String, Object>> list;
		try {
			list = jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			list = null;
		}
		if (list != null)
			for (Map<String, Object> map : list)
				content.put((String) map.get("PK_PARA_ID"), (String) map.get("PARA_VALUE"));
	}

}
